package com.geek.game.manager.agent.controller;


import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.geek.game.manager.agent.base.Constants;
import com.geek.game.manager.agent.base.mvc.BaseController;
import com.geek.game.manager.agent.base.utils.MD5Util;
import com.geek.game.manager.agent.model.ApplicationForm;
import com.geek.game.manager.agent.model.Manager;

import ydh.cicada.dao.JdbcDao;
import ydh.cicada.query.QueryObject;
import ydh.cicada.service.CommonService;
import ydh.cicada.utils.EntityUtils;

/**
 * 用户控制器
 * @author tears
 *
 */
@Controller
@RequestMapping(value="user")
public class UserController extends BaseController{

	@Autowired
	private JdbcDao dao;
	@Autowired
	private CommonService commonService;
	/**
	 * 登录页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="login", produces = "text/html;charset=utf8")
	public String login(Model model){
		model.addAttribute("gameName", "友友扎金花");
		return "login";
	}
	/**
	 * 登录方法
	 * @param userName
	 * @param pwd
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="loginIn",produces = "text/html;charset=utf8")
	public String loginMethod(String userName,String pwd,HttpServletResponse response){
		if(StringUtils.isNotBlank(userName) && StringUtils.isNotBlank(pwd)){
			Manager u=QueryObject.select("t.user_id id,t.*",Manager.class)
					.from("t_manager t")
					.cond("deltag").equ(Constants.PLAYER_USED)
					.cond("user_account").equ(userName)
					.cond("password").equ(MD5Util.GetMD5Code(pwd))
					.firstResult(dao);
			if(u!=null){ 
				returnResult.setMsg("登录成功！");
				session.setAttribute(request.getLocalAddr(), u);
			}else{ 
				returnResult.setError(true);
				returnResult.setMsg("用户名或密码错误！");
			}
		}
		return resultVal();
	}
	/**
	 * 保存代理申请
	 * @param form
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@ResponseBody
	@RequestMapping(value="saveApplicat",method = RequestMethod.POST,produces = "text/html;charset=utf8")
	public String saveApplicationForm(ApplicationForm form) throws UnsupportedEncodingException{
		if(form!=null && StringUtils.isNotBlank(form.getPlayerId()) && StringUtils.isNotBlank(form.getTelPhone())){
			Integer count=QueryObject.select("count(1)",Integer.class)
					.from("application_form")
					.cond("player_id").equ(form.getPlayerId())
					.cond("applicat_type").equ(Constants.APPLICATION_AGENT)
					.cond("watch_flag").equ(false)
					.cond("disabled").equ(false)
					.uniqueResult(dao);
			if(count>0){
				returnResult.setError(true);
				returnResult.setMsg("已经提交过申请,请等待管理员处理,勿重复提交");
				
				return resultVal();
			}
			form.setContent(new String(form.getContent().getBytes("ISO-8859-1"), "utf-8"));
			form.setName(new String(form.getName().getBytes("ISO-8859-1"), "utf-8"));
			form.setWeixin(new String(form.getWeixin().getBytes("ISO-8859-1"), "utf-8"));
			form.setCreateTime(new Date());
			form.setApplicatType(Constants.APPLICATION_AGENT);
			commonService.insert(form);
			returnResult.setMsg("保存成功");
		}else{
			returnResult.setError(true);
			returnResult.setMsg("玩家ID或者电话号码为空");
		}
		return resultVal();
	}
	/**
	 * 退出俱乐部申请
	 * @param form
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@ResponseBody
	@RequestMapping(value="saveQuitClub",method = RequestMethod.POST,produces = "text/html;charset=utf8")
	public String saveQuitClubForm(ApplicationForm form) throws UnsupportedEncodingException{
		if(form!=null && StringUtils.isNotBlank(form.getTelPhone())){
			Integer count=QueryObject.select("count(1)",Integer.class)
					.from("application_form")
					.cond("player_id").equ(form.getPlayerId())
					.cond("applicat_type").equ(Constants.APPLICATION_QUIT)
					.cond("watch_flag").equ(false)
					.cond("disabled").equ(false)
					.uniqueResult(dao);
				if(count>0){
					returnResult.setError(true);
					returnResult.setMsg("已经提交过申请,请等待管理员处理,勿重复提交");
					return resultVal();
				}
				form.setContent(new String(form.getContent().getBytes("ISO-8859-1"), "utf-8"));
				form.setName(new String(form.getName().getBytes("ISO-8859-1"), "utf-8"));
				form.setWeixin(new String(form.getWeixin().getBytes("ISO-8859-1"), "utf-8"));
			form.setCreateTime(new Date());
			form.setApplicatType(Constants.APPLICATION_QUIT);
			commonService.insert(form);
			returnResult.setMsg("保存成功");
		}else{
			returnResult.setError(true);
			returnResult.setMsg("玩家ID或者电话号码为空,保存失败");
		}
		return resultVal();
	}

}
